package com.dog.view.controller;

import com.dog.model.ContactPerson;
import com.dog.model.Customer;
import com.dog.model.Requisite;
import com.dog.service.IContactPersonService;
import com.dog.service.ICustomerService;
import com.dog.view.beans.BaseBean;
import com.dog.view.beans.CustomerBean;
import org.richfaces.component.html.HtmlTabPanel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.PostConstruct;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * User: Kuttubek Aidaraliev
 * Date: 06.04.2010
 * Time: 13:35:27
 */
@Controller
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class CustomerBeanController extends BaseBeanController<ICustomerService, Customer, CustomerBean> {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IContactPersonService contactPersonService;
    /*private String selectedContactPersonTab;
    private HtmlTabPanel contactPersonTabPanel;
    private ContactPerson contactPerson;
    private boolean editingContactPersons = false;
    private boolean changeContactPersons = false;
    private boolean showFormContactPerson = false;
    private boolean requisiteEditing = false;*/

    public CustomerBeanController() {
    }

    @Autowired
    public void setCustomerService(ICustomerService iCustomerService) {
        super.setService(iCustomerService);
    }

    @Override
    public String preUpdate() {
        loadWithContactPersons();
        return super.preUpdate();
    }

    @Override
    public String updated() {
        setChangeContactPersons(false);
        setRequisiteEditing(false);
        return super.updated();
    }

    @Override
    public String view() {
        loadWithContactPersons();
        return super.view();
    }

    @Override
    public String list() {
        return "customerList";
    }

    @Override
    protected String form() {
        return "customer";
    }

    @Override
    protected Customer newEntity() {
        Customer customer = new Customer();
        customer.setRequisite(new Requisite());
        return customer;
    }

    @Override
    protected SelectItem createSelectItem(Customer customer) {
        return new SelectItem(customer, customer.getOfficialName());
    }

    @Override
    protected String getNameEntity() {
        return "customerBean";
    }

    @Override
    protected CustomerBean createEntityBean() {
        return new CustomerBean();
    }

    public String cancel() {
        boolean creating = isNewEntity();
        setNewEntity(false);
        if (creating) return list();
        else return form();
    }

    public String currentPage(){
        return null;
    }

    public void loadWithContactPersons() {
        Customer customer = getService().getByIdWithContactPersons(getEntityBean().getEntity().getId());
        getEntityBean().setEntity(customer);
    }

    public List<SelectItem> getContactPersonsSelectList() {
        List<ContactPerson> customers = contactPersonService.getAll();
        List<SelectItem> selectItems = new LinkedList<SelectItem>();
        for (ContactPerson contactPerson : customers) {
            if (contactPerson.getCustomer() == null || getEntityBean().getEntity().getContactPersons().contains(contactPerson))
                selectItems.add(createSelectItemWithContactPerson(contactPerson));
        }

        return selectItems;
    }

    public void tabSelectedActionListener(ActionEvent event) {
         setShowFormContactPerson(false);
    }

    private SelectItem createSelectItemWithContactPerson(ContactPerson contactPerson) {
        return new SelectItem(contactPerson, contactPerson.getFullName());
    }

    public String getSelectedContactPersonTab() {
        return getEntityBean().getSelectedContactPersonTab();
    }

    public void setSelectedContactPersonTab(String selectedContactPersonTab) {
        getEntityBean().setSelectedContactPersonTab(selectedContactPersonTab);
    }

    public HtmlTabPanel getContactPersonTabPanel() {
        return getEntityBean().getContactPersonTabPanel();
    }

    public void setContactPersonTabPanel(HtmlTabPanel contactPersonTabPanel) {
        getEntityBean().setContactPersonTabPanel(contactPersonTabPanel);
    }

    public boolean isEditingContactPersons() {
        return getEntityBean().isEditingContactPersons();
    }

    public void setEditingContactPersons(boolean editingContactPersons) {
        getEntityBean().setEditingContactPersons(editingContactPersons);
    }

    public boolean isChangeContactPersons() {
        return getEntityBean().isChangeContactPersons();
    }

    public void setChangeContactPersons(boolean changeContactPersons) {
        getEntityBean().setChangeContactPersons(changeContactPersons);
    }

    public void preEditingContactPersons() {
        getEntityBean().setEditingContactPersons(true);
    }

    public void afterEditingContactPersons() {
        getEntityBean().setEditingContactPersons(false);
        setChangeContactPersons(true);
    }

    public boolean isShowFormContactPerson() {
        return getEntityBean().isShowFormContactPerson();
    }

    public void setShowFormContactPerson(boolean showFormContactPerson) {
        getEntityBean().setShowFormContactPerson(showFormContactPerson);
    }

    public ContactPerson getContactPerson() {
        return getEntityBean().getContactPerson();
    }

    public void setContactPerson(ContactPerson contactPerson) {
        getEntityBean().setContactPerson(contactPerson);
    }

    public boolean isRequisiteEditing() {
        return getEntityBean().isRequisiteEditing();
    }

    public void setRequisiteEditing(boolean requisiteEditing) {
        getEntityBean().setRequisiteEditing(requisiteEditing);
    }
}
